

//更改图片
function changePicture(index, fileDelimiter, imgDelimiter) {
    var fileobj = $("#bootstrap-table input[name='cmsActivityList[" + index + "]." + fileDelimiter + "']");
    var pass = Array.from(fileobj[0].files).map(f => /^image\/\w+$/.test(f.type)).reduce((f, c) => f && c);
    if (pass) {
        var img = $("#bootstrap-table img[id='cmsActivityList[" + index + "]." + imgDelimiter + "']");
        var file = fileobj[0].files[0];
        img.attr('src', (window.URL || window.webkitURL).createObjectURL(file));
        img.css('display', 'block');
        fileobj.css('display', 'none');
        var rows = $('#bootstrap-table').bootstrapTable('getData');
        if (fileDelimiter == 'coverFile') {
            rows[index].coverFile = file;
        } else if (fileDelimiter == 'activityPictureFiles') {
            rows[index].activityPictureFiles = Array.from(fileobj[0].files);
        }
    } else {
        $.modal.alertWarning('上传的所有文件都必须是图片！');
    }
}

// 点击图片同样上传图片
function clickInputFile(index, cssDelimiter) {
    $("#bootstrap-table input[name='cmsActivityList[" + index + "]." + cssDelimiter + "']").click();
}

// 刷新图片缓存
function refreshImgData() {
    var rows = $('#bootstrap-table').bootstrapTable('getData');
    rows.forEach((row, index) => {
        if (row.coverFile) {
            var coverImg = $("#bootstrap-table img[id='cmsActivityList[" + index + "].coverImg']");
            coverImg.attr('src', (window.URL || window.webkitURL).createObjectURL(row.coverFile));
            coverImg.css('display', 'block');
            $("#bootstrap-table input[name='cmsActivityList[" + index + "].coverFile']").css('display', 'none');
        }
        if (row.activityPictureFiles) {
            var topPic = $("#bootstrap-table img[id='cmsActivityList[" + index + "].topPicture']");
            topPic.attr('src', (window.URL || window.webkitURL).createObjectURL(row.activityPictureFiles[0]));
            topPic.css('display', 'block');
            $("#bootstrap-table input[name='cmsActivityList[" + index + "].activityPictureFiles']").css('display', 'none');
        }
    });
}
